# coding: utf-8
import sys, os

__author__ = "Jiangkai"
__version__ = "1.0.0"

reload(sys)
sys.setdefaultencoding('utf8')

import datetime
import StringIO
import time
import unittest


class GenerationReport:
    def __init__(self, filename=None, title=None, description=None, data=None):
        if filename == None:
            self.filename = u"测试报告"
        else:
            self.filename = filename

        if title == None:
            self.title = u"测试报告"
        else:
            self.title = title

        if description == None:
            self.description = u"测试报告"
        else:
            self.description = description

        self.data = data

    def pj_html(self):
        """
        Define a HTML template for report customerization and generation.

        Overall structure of an HTML report

        HTML
        +------------------------+
        |<html>                  |
        |  <head>                |
        |                        |
        |   STYLESHEET           |
        |   +----------------+   |
        |   |                |   |
        |   +----------------+   |
        |                        |
        |  </head>               |
        |                        |
        |  <body>                |
        |                        |
        |   HEADING              |
        |   +----------------+   |
        |   |                |   |
        |   +----------------+   |
        |                        |
        |   REPORT               |
        |   +----------------+   |
        |   |                |   |
        |   +----------------+   |
        |                        |
        |   ENDING               |
        |   +----------------+   |
        |   |                |   |
        |   +----------------+   |
        |                        |
        |  </body>               |
        |</html>                 |
        +------------------------+
        """

    def pj_HTML_TITLE(self):
        htmltitle = """<?xml version="1.0" encoding="UTF-8"?>
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                    <head>
                        <title>""" + self.title + """</title>
                        <meta name="generator" content="HTMLTestRunner 1.0.0"/>
                        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

                    <style type="text/css" media="screen">
                    body        { font-family: verdana, arial, helvetica, sans-serif; font-size: 80%; }
                    table       { font-size: 100%; }
                    pre         { }"""
        return htmltitle

    def pj_HTML_HEADING(self):
        return """h1 {
                                    font-size: 16pt;
                                    color: gray;
                                }
                                .heading {
                                    margin-top: 0ex;
                                    margin-bottom: 1ex;
                                }

                                .heading .attribute {
                                    margin-top: 1ex;
                                    margin-bottom: 0;
                                }

                                .heading .description {
                                    margin-top: 4ex;
                                    margin-bottom: 6ex;
                                }

                                /* -- css div popup ------------------------------------------------------------------------ */
                                a.popup_link {
                                }

                                a.popup_link:hover {
                                    color: red;
                                }

                                .popup_window {
                                    display: none;
                                    position: relative;
                                    left: 0px;
                                    top: 0px;
                                    /*border: solid #627173 1px; */
                                    padding: 10px;
                                    background-color: #E6E6D6;
                                    font-family: "Lucida Console", "Courier New", Courier, monospace;
                                    text-align: left;
                                    font-size: 8pt;
                                    width: 95%;
                                }

                                }
                                /* -- report ------------------------------------------------------------------------ */
                                #show_detail_line {
                                    margin-top: 3ex;
                                    margin-bottom: 1ex;
                                }
                                #result_table {
                                    width: 80%;
                                    border-collapse: collapse;
                                    border: 1px solid #777;
                                }
                                #header_row {
                                    font-weight: bold;
                                    color: white;
                                    background-color: #777;
                                }
                                #result_table td {
                                    border: 1px solid #777;
                                    padding: 2px;
                                }
                                #total_row  { font-weight: bold; }
                                .passClass  { background-color: #6c6; }
                                .failClass  { background-color: #c60; }
                                .errorClass { background-color: #c00; }
                                .passCase   { color: #6c6; }
                                .failCase   { color: #c60; font-weight: bold; }
                                .errorCase  { color: #c00; font-weight: bold; }
                                .hiddenRow  { display: none; }
                                .testcase   { margin-left: 2em; }
                                /* -- ending ---------------------------------------------------------------------- */
                                #ending {
                                }

                                </style>

                                </head>"""

    def pj_HTML_BODY(self, listdata):
        start_time = listdata[0][0]
        end_time = listdata[0][1]

        x = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
        y = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

        trline = r""

        for i in range(1, len(listdata)):
            trline = trline + r"""<tr><td>""" + listdata[i][0] + r"""</td><td>""" + str(
                listdata[i][1]) + r"""</td><td>""" + str(listdata[i][2]) + r"""</td><td>""" + str(
                listdata[i][3]) + r"""</td></tr>"""
        aaa = r"""<body>
                        <script language="javascript" type="text/javascript"></script>
                        <div class='heading'>
                        <h1>""" + self.title + """</h1>
                        <p class='attribute'><strong>开始时间:</strong> """ + start_time + """</p>
                        <p class='attribute'><strong>结束时间:</strong>  """ + end_time + """</p>
                        <p class='attribute'><strong>历时:</strong> """ + str(y - x) + """ </p>
                        <p class='description'>""" + self.description + """</p>
                        </div>
                        <p id='show_detail_line'>视图模式
                        <a href='javascript:showCase(0)'>简览</a>
                        <a href='javascript:showCase(1)'>失败</a>
                        <a href='javascript:showCase(2)'>所有</a>
                        </p>
                        <table id='result_table'>
                        <colgroup>
                        <col align='left' />
                        <col align='right' />
                        <col align='right' />
                        <col align='right' />
                        <col align='right' />
                        <col align='right' />
                        </colgroup>
                        <tr id='header_row'><td>指标</td><td>最小</td><td>最大</td>
                            <td>平均</td></tr>""" + trline + r"""</table></body></html>"""
        return aaa

    def generation_report(self):
        filepath = os.getcwd() + "\\ReportFile\\" + self.filename + ".html"
        fp = file(filepath, 'wb')
        fp.write(self.pj_HTML_TITLE() + self.pj_HTML_HEADING() + self.pj_HTML_BODY(listdata=self.data))


if __name__ == '__main__':
    datasj = [["2016-12-10 11:20:30", "2016-12-12 15:17:30"],
              [u"内存", "253", "1450", "582"],
              [u"CPU", "0%", "100%", ""],
              [u"帧率", "4", "60", "30"]
              ]
    x = datetime.datetime.strptime(datasj[0][0], "%Y-%m-%d %H:%M:%S")
    y = datetime.datetime.strptime(datasj[0][1], "%Y-%m-%d %H:%M:%S")

    print x, y, y - x

    a = datetime.datetime.now()
    time.sleep(3)
    b = datetime.datetime.now()

    c = b - a

    print a, b, c

    grp = GenerationReport(filename="test111111", title=u'自动化测试报告aaaaaaaaaaaaasdfs',
                           description=u'自动化测试报告,测试用例sfsfsfsfsdfsdfs', data=datasj)
    grp.generation_report()
